package com.ch.fastbuilder.model;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ch.fastbuilder.common.PageResult;

/***
 * 
 * @author 奋斗
 * 此类用户构建SQL查询条件
 */
public abstract class BuilderModel extends BaseBuilderModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String SPACE = " ";
	private static final String INNER_JOIN = " INNER JOIN ";
	private static final String LEFT_JOIN = " LEFT JOIN ";
	private static final String RIGHT_JOIN = " RIGHT JOIN ";
	private static final String AND = " AND ";
	private static final String OR = " OR ";
	private static final String ORDER_BY = " ORDER BY ";
	
	public static BaseBuilderModel SQL(String strSQL) {
		BaseBuilderModel model = new Model();
		model.setSql(strSQL);
		return model;
	}
	
	public BaseBuilderModel sql(String strSQL) {
		this.setSql(strSQL);
		return this;
	}
	
	public BaseBuilderModel withParams(Map<String,Object> params) {
		this.params.putAll(params);
		return this;
	}
	
	public static BuilderModel Select(String columns) {
		BuilderModel model = new Model();
		model.setColumns(columns);

		return model;
	}
	
	public BuilderModel select(String columns) {
		this.setColumns(columns);
		return this;
	}
	
	public static BuilderModel From(String tableName) {
		BuilderModel model = new Model();
		model.from(tableName);
		return model;
	}
	
	public BuilderModel from(String tableName) {
		this.setTableName(tableName);
		return this;
	}
	
	public BuilderModel innerJoin(String strInnerJoin) {
		String strJoin = this.getJoin();
		if(strJoin == null) strJoin = "";
		
		StringBuffer sbJoin = new StringBuffer(strJoin);
		sbJoin.append(SPACE).append(INNER_JOIN).append(strInnerJoin).append(SPACE);
		
		this.setJoin(sbJoin.toString());
		return this;
	} 
	
	public BuilderModel leftJoin(String strLeftJoin) {
		String strJoin = this.getJoin();
		if(strJoin == null) strJoin = "";
		
		StringBuffer sbJoin = new StringBuffer(strJoin);
		sbJoin.append(SPACE).append(LEFT_JOIN).append(strLeftJoin).append(SPACE);
		
		this.setJoin(sbJoin.toString());
		return this;
	} 
	
	public BuilderModel rightJoin(String strRightJoin) {
		String strJoin = this.getJoin();
		if(strJoin == null) strJoin = "";
		
		StringBuffer sbJoin = new StringBuffer(strJoin);
		sbJoin.append(SPACE).append(RIGHT_JOIN).append(strRightJoin).append(SPACE);
		
		this.setJoin(sbJoin.toString());
		return this;
	} 
	
	public static BuilderModel Where(String where) {
		BuilderModel model = new Model();
		model.where(where);
		return model;
	}
	
	public static BuilderModel Where(String where, Map<String, Object> params) {
		BuilderModel model = new Model();
		model.where(where, params);
		return model;
	}
	
	public BuilderModel where(String where) {
		this.setWhere(where);
		return this;
	}
	
	public BuilderModel where(String where, Map<String, Object> params) {
		this.setWhere(where);
		this.setParams(params);
		return this;
	}
	
	public BuilderModel and(String and) {
		String strAnd = this.getAnd();
		if(strAnd == null) strAnd = "";
		
		StringBuffer sbAnd = new StringBuffer(strAnd);
		sbAnd.append(SPACE).append(AND).append(and).append(SPACE);
		
		this.setAnd(sbAnd.toString());
		return this;
	}
	
	public BuilderModel or(String or) {
		String strOr = this.getOr();
		if(strOr == null) strOr = "";
		
		StringBuffer sbAnd = new StringBuffer(strOr);
		sbAnd.append(SPACE).append(OR).append(or).append(SPACE);
		
		this.setAnd(sbAnd.toString());
		return this;
	}
	
	public BuilderModel orderBy(String order) {
		StringBuffer sbOrderBy = new StringBuffer();
		sbOrderBy.append(SPACE).append(ORDER_BY).append(order).append(SPACE);
		
		this.setOrderBy(sbOrderBy.toString());
		return this;
	} 
	
	public static BuilderModel Limit(int offset, int limit) {
		BuilderModel model = new Model();
		model.limit(offset, limit);
		return model;
	}
	
	public BuilderModel limit(int offset, int limit) {
		this.offset = offset;
		this.limit = limit;
		return this;
	}
	
	public static BuilderModel Page(int pageIndex, int pageSize) {
		BuilderModel model = new Model();
		model.page(pageIndex, pageSize);
		return model;
	}
	
	public BuilderModel page(int pageIndex, int pageSize) {
		this.offset = (pageIndex-1) * pageSize;
		this.limit = pageSize;
		return this;
	}
	
	public static BuilderModel InitParams(Map<String, Object> params) {
		BuilderModel model = new Model();
		model.setParameters(params);
		return model;
	}
	
	public BuilderModel initParams(Map<String, Object> params) {
		this.setParameters(params);
		return this;
	}
	
	public static BuilderModel SetParameters(Map<String, Object> params) {
		BuilderModel model = new Model();
		model.setParameters(params);
		return model;
	}
	
	public BuilderModel setParameters(Map<String, Object> params) {
		this.params.putAll(params);
		//setPages();
		return this;
	}
	
	public abstract <T extends Model> PageResult findPage(Class<T> classType);
	
	//静态方法，交给子类重写
	public static <T extends Model> List<T> List(Class<T> classType) {
		return null;
	}
	
	public static <T extends Model> int Count(Class<T> classType) {
		return 0;
	}
	
	public static <T extends Model> PageResult FindPage(Class<T> classType) {
		return null;
	}
}
